﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace ServeNHL.Models
{
    public class FamilyViewModel
    {
        public FAMILY family { get; set; }
        public List<UserViewModel> userViewModels { get; set; }

        /// <summary>
        /// Given a user, checks if they have a family ID and sets it to null. Also checks if they are a family head and sets that to null as well.
        /// </summary>
        /// <param name="user">User to remove from family</param>
        /// <returns>Status message.</returns>
        public string RemoveFromFamily(USER user)
        {
            string message = "Failed to remove from family.";

            // Check if the user has a family ID, if they do set it to null.
            using (var dbconn = new fclick_dbEntities())
            {
                var query = (from u in dbconn.USERs
                             where u.USER_ID == user.USER_ID
                             select u).FirstOrDefault();
                if (query.FAMILY_ID == null)
                {
                    return message;
                }
                else
                {
                    query.FAMILY_ID = null;
                }

                // Check if the user is a family_head1 or family_head2. If they are, set it to null.
                var query2 = (from f in dbconn.FAMILies
                              where f.FAMILY_HEAD1 == user.USER_ID || f.FAMILY_HEAD2 == user.USER_ID
                              select f).FirstOrDefault();

                if (query2.Equals(null))
                {
                    message = "Removed from family, user was not a family head.";
                    dbconn.SaveChanges();
                    return message;
                }
                else
                {
                    if (query2.FAMILY_HEAD1 == user.USER_ID)
                    {
                        query2.FAMILY_HEAD1 = null;
                    }
                    else
                    {
                        query2.FAMILY_HEAD2 = null;
                    }

                    // If there are no family heads, get rid of the family.
                    if (query2.FAMILY_HEAD1 == null && query2.FAMILY_HEAD2 == null)
                    {
                        dbconn.FAMILies.Remove(query2);
                    }

                    message = "Removed from family. Removed as family head.";
                    dbconn.SaveChanges();
                    return message;
                }
            }
        } // End of public string RemoveFromFamily(USER user)


        /// <summary>
        /// Adds a user to a family. If no family is given, it will create a family and assign the family head.
        /// </summary>
        /// <param name="user">User to add</param>
        /// <param name="isFamilyHead">If true, the user will be added as a family head if one of them is null.</param>
        /// <param name="family">Family to add user to.</param>
        public void addToFamily(USER user, Boolean isFamilyHead, FAMILY family = null)
        {
            using (var dbconn = new fclick_dbEntities())
            {
                var getUser = (from u in dbconn.USERs
                          where u.USER_ID == user.USER_ID
                          select u).FirstOrDefault();

                USER usr = getUser;

                if (family == null)
                {
                    CreateFamily(user);
                }
                else
                {
                    if (isFamilyHead)
                    {
                        var query = (from f in dbconn.FAMILies
                                     where f.FAMILY_ID == family.FAMILY_ID
                                     select f).FirstOrDefault();

                        FAMILY fam = query;

                        if(fam.FAMILY_HEAD1 == null)
                        {
                            fam.FAMILY_HEAD1 = user.USER_ID;
                        }
                        else if( fam.FAMILY_HEAD2 == null)
                        {
                            fam.FAMILY_HEAD2 = user.USER_ID;
                        }
                    }
                }
                usr.FAMILY_ID = family.FAMILY_ID;
            }
        }



        /*  I have no idea if this will work, but its something to consider. Can put this function inside the add to family method call.  */

        /// <summary>
        /// Creates a user and adds to the family. 
        /// </summary>
        /// <param name="email">Email of person to add</param>
        /// <param name="password">Password of user</param>
        /// <param name="firstName">First name of user</param>
        /// <param name="lastName">Last name of user</param>
        /// <param name="phone">Phone number of user</param>
        public USER AddFamilyMember(string email, string password, string firstName, string lastName, string phone)
        {
            UserViewModel u = new UserViewModel();

            USER user = new USER();
            user.USERNAME = email;
            user.PASSWORD = Helpers.UserHelper.getPasswordSaltHash(password);
            user.STATUS = "Active";
            user.FAMILY_ID = this.family.FAMILY_ID;

            USER_CONTACT contact = new USER_CONTACT();
            contact.PHONE = phone;
            contact.EMAIL = email;
            contact.FIRST_NAME = firstName;
            contact.LAST_NAME = lastName;
            
            u.user.InsertUser(user);
            u.contact.UserContactInsert(contact);

            return user;
        }

        /// <summary>
        /// Create a family based on a user (for the name). Adds the user as the family head.
        /// </summary>
        /// <param name="user">User to create family from.</param>
        public void CreateFamily(USER user)
        {
            using (var dbconn = new fclick_dbEntities())
            {
                FAMILY fam = new FAMILY();

                var query = (from uc in dbconn.USER_CONTACT
                             where uc.USER_ID == user.USER_ID
                             select uc).FirstOrDefault();

                // Or however you want to name them
                fam.FAMILY_NAME = query.FIRST_NAME + query.LAST_NAME;
                fam.FAMILY_HEAD1 = user.USER_ID;
                fam.FAMILY_HEAD2 = null;

                var userQ = (from u in dbconn.USERs
                             where u.USER_ID == user.USER_ID
                             select u).FirstOrDefault();

                userQ.FAMILY_ID = fam.FAMILY_ID;

                dbconn.FAMILies.Add(fam);
                dbconn.SaveChanges();
            }
        }


        /// <summary>
        /// Given a search term, returns a list of all families that contain the search phrase.
        /// </summary>
        /// <param name="search">Name to search for</param>
        /// <returns>List of families containing the search phrase in its name.</returns>
        public List<FAMILY> searchFamily(string search)
        {
            using (var dbconn = new fclick_dbEntities())
            {
                var query = (from f in dbconn.FAMILies
                             where f.FAMILY_NAME.Contains(search)
                             select f).ToList();

                return query;
            }
        }



        /// <summary>
        /// Given a family ID number, returns the family.
        /// </summary>
        /// <param name="ID">Family to retrieve family</param>
        /// <returns>Family associated with the family ID</returns>
        public FAMILY searchFamily(int ID)
        {
            using (var dbconn = new fclick_dbEntities())
            {
                var query = (from f in dbconn.FAMILies
                             where f.FAMILY_ID == ID
                             select f).FirstOrDefault();

                return query;
            }
        }



    }//End of Family View Model
}